/*****************************************************************
*
*	Netcare Agent Project
*		Copyright(C) 2010, Wicresoft EKA. All Rights Reserved.
*
*	Authors:
*		Parker Zhou (parkerz@wicresoft.com)
*
*	Part of the product code depends on gSOAP under GPL license
*		Copyright(C) 2000-2010, Robert van Engelen, Genivia Inc. All Rights Reserved.
*
*****************************************************************/
#ifndef _SERVICEOPERATION_H_
#define _SERVICEOPERATION_H_


#pragma once

#include <vector>
#include <string>
#include "ns.h"


#ifdef WIN32
#include <windows.h>
#pragma comment(lib, "advapi32.lib")
#else
#include <string.h>
#endif

NETCARE_AGENT;
USING_STD;

enum ServiceStatus
{
	service_stop,
    service_stop_pending,
    service_start_pending,
    service_running,
    service_pause,
    service_pause_pending,
    service_continue_pending,
    service_unknow
};

class CSysService
{
public:
#ifdef WIN32
	CSysService(int pid , ServiceStatus status , const wchar_t* shortName ,const wchar_t* displayName);
	CSysService(const wchar_t* shortName);
#else
	CSysService(int pid , ServiceStatus status , const char* shortName , const char* displayName);
	CSysService(const char* shortName);
#endif
	~CSysService();
	int PID;
	ServiceStatus Status;
#ifdef WIN32
	wstring ShortName;
	wstring DisplayName;
#else
	string ShortName;
	string DisplayName;
#endif
};

//config the excluded for linux service from etc/init.d
#ifndef WIN32
static char* excluded[] = { "README","halt","rc","rcS","reboot","single","skeleton" };
static int excludedNumber = 7;
#endif

class CServiceOperation
{
public:
	CServiceOperation(void);
	~CServiceOperation(void);
	static vector<CSysService*>* GetServices();
#ifdef WIN32
	bool RestartService(LPCWSTR aServiceName);
private:
	bool DoStartSvc(LPCWSTR szSvcName);
	bool DoStopSvc(LPCWSTR szSvcName);
	bool StopDependentServices(SC_HANDLE schSCManager,SC_HANDLE schService);
private:
	static ServiceStatus ConvertStatus(DWORD dwCurrentState);

#else
	bool RestartService(string aServiceName);
#endif
};
ENDNS ENDNS


#endif
